package send_message

import (
	"bytes"
	"encoding/json"
	"net/http"
)

type WechatInterface interface {
	SendMessageToWechat(string, string) error
}

type Text struct {
	Content string `json:"content"`
}

type WechatMessageImpl struct {
	MsgType string `json:"msgtype"`
	Text    Text   `json:"text"`
}

func SendMessageToWechat(webhook string, msg string) error {
	wechat := WechatMessageImpl{
		MsgType: "text",
	}
	wechat.Text.Content = msg
	jsonMessage, err := json.Marshal(wechat)
	if err != nil {
		return err
	}

	resp, err := http.Post(webhook, "application/json", bytes.NewBuffer(jsonMessage))
	if err != nil {
		return err
	}
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		return err
	} else {
		return nil
	}
}
